package ph.com.gs3.formalistics.presenter.fragments.views;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.model.vo.business.User;
import ph.com.gs3.formalistics.services.managers.ImageManager;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class UserProfileViewFragment extends Fragment {

	private User userOnDisplay;

	private ImageView ivAvatar;

	private TextView tvDisplayName;
	private TextView tvCompanyName;
	private TextView tvEmail;
	private TextView tvConnection;

	private Button bLogout;

	private ImageManager imageManager;

	private UserProfileViewActionListener listener;

	public static UserProfileViewFragment createInstance(User userOnDisplay) {

		UserProfileViewFragment instance = new UserProfileViewFragment();
		instance.setUserOnDisplay(userOnDisplay);
		return instance;

	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		try {
			listener = (UserProfileViewActionListener) activity;
		} catch (ClassCastException e) {
			throw new IllegalStateException(activity.getClass().getSimpleName()
			        + " must implement UserProfileViewActionListener");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_user_profile, container, false);

		// initialize views
		ivAvatar = (ImageView) rootView.findViewById(R.id.UserProfile_ivAvatar);
		tvDisplayName = (TextView) rootView.findViewById(R.id.UserProfile_tvDisplayName);
		tvCompanyName = (TextView) rootView.findViewById(R.id.UserProfile_tvCompanyName);
		tvEmail = (TextView) rootView.findViewById(R.id.UserProfile_tvEmail);
		tvConnection = (TextView) rootView.findViewById(R.id.UserProfile_tvConnection);

		bLogout = (Button) rootView.findViewById(R.id.UserProfile_bLogout);

		assignUserDataToView();
		loadUserImage();

		bLogout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (listener != null) {
					listener.onLogoutCommand();
				}
			}
		});

		return rootView;
	}

	private void assignUserDataToView() {
		tvDisplayName.setText(userOnDisplay.getDisplayName());
		tvCompanyName.setText(userOnDisplay.getCompany().getName());
		tvEmail.setText(userOnDisplay.getEmail());
		tvConnection.setText("Connected to: " + userOnDisplay.getCompany().getServer());

	}

	private void loadUserImage() {
		imageManager = ImageManager.getDefaultInstance(getActivity().getApplicationContext());
		imageManager.requestUserImage(userOnDisplay.getId(), ivAvatar);
	}

	public User getUserOnDisplay() {
		return userOnDisplay;
	}

	public void setUserOnDisplay(User userOnDisplay) {
		this.userOnDisplay = userOnDisplay;
	}

	public static interface UserProfileViewActionListener {

		public void onLogoutCommand();

	}

}
